package com.cls.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.cls.business.entity.Message;
import com.cls.business.service.IMessageService;
import com.cls.common.annotation.ControllerEndpoint;
import com.cls.common.controller.BaseController;
import com.cls.common.entity.MyConstant;
import com.cls.common.entity.QueryRequest;
import com.cls.common.entity.ResponseVO;
import com.cls.common.utils.MyUtil;
import com.github.pagehelper.PageInfo;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 系统消息表 Controller
 *
 * @author wandering
 * @date 2020-11-27 10:39:09
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class MessageController extends BaseController {

    private final IMessageService messageService;


    /**
     * 系统消息表修改页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/message/messageUpdate/{messageId}")
    public String messageUpdate(@PathVariable Long messageId, Model model) {
        Message message = messageService.getById(messageId);
        model.addAttribute("message", message);
        return MyUtil.view("business/message/messageUpdate");
    }


    /**
     * 系统消息表详情页面
     *
     * @return
     */
    @GetMapping(MyConstant.VIEW_PREFIX + "biz/message/messageDetail/{messageId}")
    public String messageDetail(@PathVariable Long messageId, Model model) {
        Message message = messageService.getById(messageId);
        model.addAttribute("message", message);
        return MyUtil.view("business/message/messageDetail");
    }

    @GetMapping("message")
    @ResponseBody
    @RequiresPermissions("message:list")
    public ResponseVO getAllMessages(Message message) {
        return ResponseVO.success(messageService.findMessages(message));
    }

    @GetMapping("message/list")
    @ResponseBody
//    @RequiresPermissions("message:list")
    public ResponseVO messageList(QueryRequest request, Message message) {
        PageInfo<Message> pageInfo = this.messageService.findMessages(request, message);
        return ResponseVO.success(pageInfo);
    }

    /**
     * 查看专家和申请人接受消息
     *
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/27 11:22 上午
     */
    @GetMapping("message/client/list")
    @ResponseBody
//    @RequiresPermissions("message:list")
    public ResponseVO messageClientList(QueryRequest request, Message message) {
        message.setReceiveUserId(getCurrentUser().getUserId());
        PageInfo<Message> pageInfo = this.messageService.findMessages(request, message);
        return ResponseVO.success(pageInfo);
    }

    /**
     * 查看专家和申请人接受消息
     *
     * @return a
     * @author Carey
     * @description
     * @date 2020/11/27 11:22 上午
     */
    @GetMapping("message/client/unreadCount")
    @ResponseBody
    public ResponseVO messageClientUnreadCount() {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Message::getReceiveUserId, getCurrentUser().getUserId());
        queryWrapper.eq(Message::getIsRead, false);

        return ResponseVO.success(this.messageService.count(queryWrapper));
    }

    @ControllerEndpoint(operation = "新增Message", exceptionMessage = "新增Message失败")
    @PostMapping("message")
    @ResponseBody
//    @RequiresPermissions("message:add")
    public ResponseVO addMessage(@Valid Message message) {
        this.messageService.createMessage(message);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "多个删除Message", exceptionMessage = "删除Message失败")
    @PostMapping("message/deletes/{ids}")
    @ResponseBody
    @RequiresPermissions("message:delete")
    public ResponseVO deleteMessages(@PathVariable String ids) {
        String[] idArry = ids.split(StringPool.COMMA);
        this.messageService.deleteMessages(idArry);
        return ResponseVO.success();
    }


    @ControllerEndpoint(operation = "删除单个Message", exceptionMessage = "删除Message失败")
    @PostMapping("message/delete/{messageId}")
    @ResponseBody
    @RequiresPermissions("message:delete")
    public ResponseVO deleteMessage(@PathVariable Long messageId) {
        this.messageService.removeById(messageId);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Message", exceptionMessage = "修改Message失败")
    @PostMapping("message/update")
    @ResponseBody
    @RequiresPermissions("message:update")
    public ResponseVO updateMessage(Message message) {
        this.messageService.updateMessage(message);
        return ResponseVO.success();
    }

    @ControllerEndpoint(operation = "修改Message", exceptionMessage = "导出Excel失败")
    @PostMapping("message/excel")
    @ResponseBody
    @RequiresPermissions("message:export")
    public void export(QueryRequest queryRequest, Message message, HttpServletResponse response) {
        List<Message> messages = this.messageService.findMessages(queryRequest, message).getList();
        ExcelKit.$Export(Message.class, response).downXlsx(messages, false);
    }
}
